package com.bloom.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.bloom.dao.StaticDataDao;
import com.bloom.entity.DisplayInfo;
import com.bloom.entity.ServiceProvider;
import com.bloom.entity.ServiceReceiver;
import com.bloom.entity.User;
import com.bloom.entity.UserTravelDetails;
import com.bloom.model.TravelInfo;

public class FormToEntity {

	public static UserTravelDetails toUserTravelDetails(TravelInfo travelInfo) {
		if (travelInfo == null) {
			return null;
		}
		UserTravelDetails details = new UserTravelDetails();
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = (Date) formatter.parse(travelInfo.getDate());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		details.setDateTime(cal);
		details.setFlightNo(travelInfo.getNo());
		DisplayInfo mode = new DisplayInfo();
		mode.setId(Integer.valueOf(travelInfo.getMode()));
		details.setMode(mode);

		return details;
	}

	public static ServiceProvider toServiceProvider(int travelId,
			String username) {
		ServiceProvider serviceProvider = new ServiceProvider();
		UserTravelDetails travelDetails = new UserTravelDetails();
		travelDetails.setId(travelId);
		serviceProvider.setTravelDetails(travelDetails);
		User user = new User();
		user.setUsername(username);
		serviceProvider.setUser(user);
		return serviceProvider;
	}

	public static ServiceReceiver toServiceReceiver(int travelId,
			String username) {
		ServiceReceiver serviceReceiver = new ServiceReceiver();
		UserTravelDetails travelDetails = new UserTravelDetails();
		travelDetails.setId(travelId);
		serviceReceiver.setTravelDetails(travelDetails);
		User user = new User();
		user.setUsername(username);
		serviceReceiver.setUser(user);
		return serviceReceiver;
	}

}
